package svgedit.japangui.actions;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

import svgedit.japangui.JapanFrame;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;

/** Combines selected elements into a group.
 */
@SuppressWarnings("serial")
public class GroupAction extends AbstractAction {

	private JapanFrame japanFrame;

	/** Create this action for the given frame */
	public GroupAction(JapanFrame japanFrame) {
		super("\u30B0\uFF99\u30FC\u30D7");
		this.japanFrame = japanFrame;
	}

	public void actionPerformed(ActionEvent ae) {
		SVGElement elems[] = japanFrame.getView().getSelectedElements();

		// Remove elems from their current parent and add to a new
		// group.
		// TODO: Z-ordering is broken by this operation; group should be
		//   inserted where the first child was
		if (elems.length > 1) {
			SVGGroup group = new SVGGroup(japanFrame.getDocument());
			for (SVGElement elem : elems) {
				elem.getParent().removeChild(elem);
				group.appendChild(elem);
			}
			japanFrame.getDocument().getRootGroup().appendChild(group);
			japanFrame.getView().getDocument().setModified(true);
			japanFrame.getView().setSelectedElement(group);
		}
	}


}
